package cn.mickey.exchange.api;


import cn.mickey.exchange.annotation.RedisCache;
import cn.mickey.exchange.domain.JsonResponse;
import cn.mickey.exchange.domain.model.AdminModel;
import cn.mickey.exchange.domain.model.IdleItemModel;
import cn.mickey.exchange.domain.model.UserModel;
import cn.mickey.exchange.service.AdminService;
import cn.mickey.exchange.service.IdleItemService;
import cn.mickey.exchange.service.OrderService;
import cn.mickey.exchange.service.UserService;
import cn.mickey.exchange.util.PageUtil;
import cn.mickey.exchange.util.RSAUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RedisCache
@RestController
@RequestMapping("/admin")
public class AdminApi {

    @Resource
    private AdminService adminService;

    @Resource
    private IdleItemService idleItemService;

    @Resource
    private OrderService orderService;

    @Resource
    private UserService userService;

    @PostMapping("/login")
    public JsonResponse login(@RequestBody AdminModel adminModel,
                              HttpSession session) throws Exception {
        AdminModel dbAdmin = adminService.login(adminModel.getAccountNumber());
        if (null != dbAdmin) {
            if (adminModel.getAdminPassword()
                    .equals(RSAUtil.decrypt(dbAdmin.getAdminPassword()))) {
                session.setAttribute("admin", adminModel);
                return JsonResponse.success(dbAdmin);
            } else {
                return JsonResponse.fail("密码错误!");
            }
        }
        return JsonResponse.fail("管理员账号不存在!");
    }

    @GetMapping("/loginOut")
    public JsonResponse loginOut(HttpSession session) {
        session.removeAttribute("admin");
        return JsonResponse.success();
    }

    @PostMapping("/add")
    public JsonResponse addAdmin(HttpSession session,
                                 @RequestBody AdminModel adminModel) throws Exception {
        if (session.getAttribute("admin") == null) {
            return JsonResponse.fail();
        }
        adminModel.setAdminPassword(RSAUtil.encrypt(adminModel.getAdminPassword()));
        if (adminService.addAdmin(adminModel)) {
            return JsonResponse.success();
        }
        return JsonResponse.fail("添加失败!");
    }

    @GetMapping("/list")
    public JsonResponse getAdminList(HttpSession session,
                                     @RequestParam(value = "page", required = false) Integer page,
                                     @RequestParam(value = "nums", required = false) Integer nums) {
        if (session.getAttribute("admin") == null) {
            return JsonResponse.fail();
        }
        return JsonResponse.success(adminService
                .getAdminList(PageUtil.getPage(page), PageUtil.getNum(nums)));
    }

    @GetMapping("/userList")
    public JsonResponse userList(HttpSession session,
                                 @RequestParam(value = "page", required = false) Integer page,
                                 @RequestParam(value = "nums", required = false) Integer nums,
                                 @RequestParam("status") Integer status) {
        if (session.getAttribute("admin") == null) {
            return JsonResponse.fail();
        }
        return JsonResponse.success(userService
                .getUserByStatus(status, PageUtil.getPage(page), PageUtil.getNum(nums)));
    }

    @GetMapping("/updateUserStatus")
    public JsonResponse updateUserStatus(HttpSession session,
                                         @RequestParam("userId") Long userId,
                                         @RequestParam("status") Integer status) {
        if (session.getAttribute("admin") == null) {
            return JsonResponse.fail();
        }
        if (userService.updateUserInfo(new UserModel().setUserId(userId)
                .setUserStatus(status.byteValue())))
            return JsonResponse.success();
        return JsonResponse.fail("操作失败!");
    }

    @GetMapping("/idleList")
    public JsonResponse idleList(HttpSession session,
                                 @RequestParam("status") Integer status,
                                 @RequestParam(value = "page", required = false) Integer page,
                                 @RequestParam(value = "nums", required = false) Integer nums) {
        if (session.getAttribute("admin") == null) {
            return JsonResponse.fail();
        }
        return JsonResponse.success(idleItemService
                .adminGetIdleList(status, PageUtil.getPage(page), PageUtil.getNum(nums)));
    }

    @GetMapping("/updateIdleStatus")
    public JsonResponse updateIdleStatus(HttpSession session,
                                         @RequestParam("userId") Long userId,
                                         @RequestParam("idleId") Long idleId,
                                         @RequestParam("status") Integer status) {
        if (session.getAttribute("admin") == null) {
            return JsonResponse.fail();
        }
        IdleItemModel idleItemModel = new IdleItemModel();
        idleItemModel.setIdleId(idleId).setUserId(userId).setIdleStatus(status);
        if (idleItemService.updateIdleItem(idleItemModel)) {
            return JsonResponse.success();
        }
        return JsonResponse.fail();
    }

    @GetMapping("/orderList")
    public JsonResponse orderList(HttpSession session,
                              @RequestParam(value = "page", required = false) Integer page,
                              @RequestParam(value = "nums", required = false) Integer nums) {
        if (session.getAttribute("admin") == null) {
            return JsonResponse.fail();
        }
        return JsonResponse.success(orderService.getAllOrder(PageUtil.getPage(page), PageUtil.getNum(nums)));
    }

    @GetMapping("/deleteOrder")
    public JsonResponse deleteOrder(HttpSession session,
                                @RequestParam("id")  Long id) {
        if (session.getAttribute("admin") == null) {
            return JsonResponse.fail();
        }
        if (orderService.deleteOrder(id)) {
            return JsonResponse.success();
        }
        return JsonResponse.fail();
    }

}
